package test06;

import java.util.ArrayList;
import java.util.List;

public class CompositeOptimizationStrategy implements InferenceOptimizationStrategy {
    private List<InferenceOptimizationStrategy> strategies = new ArrayList<>();

    @Override
    public Tensor[] optimizeInference(Model model, RequestData[] requests) {
        System.out.println("🔄 应用组合优化策略链");

        Tensor[] currentOutputs = null;
        Model currentModel = model;

        for (InferenceOptimizationStrategy strategy : strategies) {
            if (currentOutputs == null) {
                // 第一次执行使用原始请求
                currentOutputs = strategy.optimizeInference(currentModel, requests);
            } else {
                // 后续策略处理前一个策略的输出（模拟）
                RequestData[] simulatedRequests = createSimulatedRequests(currentOutputs);
                currentOutputs = strategy.optimizeInference(currentModel, simulatedRequests);
            }
        }

        return currentOutputs;
    }

    private RequestData[] createSimulatedRequests(Tensor[] outputs) {
        RequestData[] requests = new RequestData[outputs.length];
        for (int i = 0; i < outputs.length; i++) {
            requests[i] = new RequestData("processed_input_" + i);
        }
        return requests;
    }

    public void addStrategy(InferenceOptimizationStrategy strategy) {
        strategies.add(strategy);
        System.out.println("添加策略: " + strategy.getClass().getSimpleName());
    }

    public void removeStrategy(InferenceOptimizationStrategy strategy) {
        strategies.remove(strategy);
        System.out.println("移除策略: " + strategy.getClass().getSimpleName());
    }

    public List<InferenceOptimizationStrategy> getStrategies() {
        return new ArrayList<>(strategies);
    }
}
